include .env
namespace?=bi-service
export

.PHONY: install delete build config docker_login
docker_login:
	@docker login -u ${DOCKER_USER} -p ${DOCKER_PASS} registry.cn-hangzhou.aliyuncs.com

docker_reg:
	kubectl create secret docker-registry \
		wenjie-alicloud \
    	--docker-server=registry.cn-hangzhou.aliyuncs.com \
    	--docker-username=${DOCKER_USER} \
    	--docker-password="${DOCKER_PASS}" \
  		-n ${namespace} --dry-run=client -oyaml | kubectl apply -f -

install_mysql:
	kubectl apply -f mysql.yaml -n ${namespace}

delete_mysql:
	kubectl delete -f mysql.yaml -n ${namespace}

install_hive: config
	kubectl apply -f hive.yaml -n ${namespace}

delete_hive:
	kubectl delete -f hive.yaml -n ${namespace}
	kubectl delete secret hive-conf -n ${namespace}

build:
	docker build -t registry.cn-hangzhou.aliyuncs.com/wenjie-base/hive:3.1.3 .
	docker push registry.cn-hangzhou.aliyuncs.com/wenjie-base/hive:3.1.3

config:
	-mkdir -p build/conf
	for name in conf/*; do\
		ESCAPE=$$ envsubst < $${name} > ./build/$${name}; \
    done
	files=" "; \
	for name in ./build/conf/*; do\
		file=$$(echo $$name | awk '{print $$NF}' FS="/"); \
		arg=$$(echo "--from-file=$$file=$$name"); \
		files="$$files $$arg"; \
    done;\
	kubectl create secret generic \
		hive-conf \
		$$files \
		-n ${namespace} --dry-run=client -o yaml | kubectl apply -f -

run_test:
	kubectl run -it --rm --restart=Never --image=busybox busybox --namespace=${namespace} -- /bin/sh